import { OhosPluginId, OhosAppContext } from '@ohos/hvigor-ohos-plugin'
import { FileUtil, HvigorNode } from '@ohos/hvigor'
import * as child_process from 'child_process'
import * as os from 'os'

/**
 * gitHooks 任务实现
 * 执行git hooks命令
 */
export function preCommit(node: HvigorNode) {
  const platform = os.platform();
  const appContext: OhosAppContext = node.getContext(OhosPluginId.OHOS_APP_PLUGIN);
  const hooksPath = FileUtil.pathResolve(appContext.getProjectPath(), 'hooks');
  let command = 'git config core.hooksPath ' + hooksPath;
  if (platform === 'win32') {
    child_process.execSync(command);
  } else if (platform === 'darwin') {
    command += ' && chmod 700 ' + hooksPath + '/*';
    child_process.execSync(command);
  }
}

